package caiziyou.com.juc.volatiles;

import java.util.concurrent.TimeUnit;

/**
 * volatile 可见性
 * 
 * @author Administrator
 *
 */
public class MyData2 {
	volatile int number = 0;

	public void addT060() {
		this.number = 60;
	}

	public static void main(String[] args) {
		MyData2 myData = new MyData2();
		new Thread(() -> {
			System.out.println(Thread.currentThread().getName() + "\t come in");
			try {
				TimeUnit.SECONDS.sleep(3);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			myData.addT060();
			System.out.println(Thread.currentThread().getName() + "\t update number value = " + myData.number);

		}, "AAA").start();
		// 如果number 被volatile 修饰
		// 当工作空间内存中的值发生变化时写入主内存空间，并通知各个线程更新数据
		while (myData.number == 0) {

		}
		// 下面的代码不会执行
		System.out.println(Thread.currentThread().getName() + " final number number " + myData.number);

	}
}